﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace SecurityDev
{
    public partial class Statistic : Form
    {
        DataTable table;
        string[] columnArr;
        bool allow = false;

        public Statistic()
        {
            InitializeComponent();
        }

        private void Statistic_Load(object sender, EventArgs e)
        {
            LoadChartTypes(chartTypeComboBox);
            regionsRadioButton.Checked = true;
        }
                
        private void LoadChartTypes(ComboBox comboBox)
        {
            comboBox.Items.Add(SeriesChartType.Column.ToString());
            comboBox.Items.Add(SeriesChartType.Bar.ToString());
            comboBox.Items.Add(SeriesChartType.Area.ToString());
            comboBox.Items.Add(SeriesChartType.SplineArea.ToString());
            comboBox.Items.Add(SeriesChartType.Line.ToString());
            comboBox.Items.Add(SeriesChartType.Pie.ToString());
            comboBox.Items.Add(SeriesChartType.Doughnut.ToString());
            comboBox.Items.Add(SeriesChartType.Point.ToString());
            comboBox.Items.Add(SeriesChartType.Bubble.ToString());
            comboBox.Items.Add(SeriesChartType.Spline.ToString());
            comboBox.Items.Add(SeriesChartType.Renko.ToString());
            comboBox.Items.Add(SeriesChartType.ThreeLineBreak.ToString());
            comboBox.Items.Add(SeriesChartType.Radar.ToString());            
            comboBox.SelectedIndex = 0;
            allow = true;
            
        }
        
        private void AllTimeCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (allTimeCheckBox.Checked == true)
            {
                dateTimePicker1.Enabled = false;
                dateTimePicker2.Enabled = false;
            }
            else
            {
                dateTimePicker1.Enabled = true;
                dateTimePicker2.Enabled = true;    
            }
            regionsRadioButton_CheckedChanged(this, new EventArgs());
        }

        private void regionsRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (regionsRadioButton.Checked)
            {
                regionSelGropbox.Visible = false;

                string fileStr1 = "Regions";
                if (!System.IO.File.Exists(fileStr1))
                {
                    return;
                }

                table = new DataTable();
                table.Columns.AddRange(new DataColumn[] 
                { new DataColumn("Region", typeof(string)),
                    new DataColumn("Quantity", typeof(int)) });

                string[] regionArray = System.IO.File.ReadAllLines(fileStr1);

                DataRow row;
                foreach (string  region in regionArray)
                {
                    row = table.NewRow();
                    row[0] = region;
                    row[1] = (from a in DS.outpost.alert
                            where a.dvrRow.objectRow.obj_region == region &&
                            CheckedTimePickers(a)
                            select a).Count();

                    table.Rows.Add(row);
                }
                columnArr = new string[] { "Region", "Quantity" };
                chartPanel.Controls.Clear();
                chartPanel.Controls.Add(GetChart(columnArr));
            }
            else
            {
                regionSelGropbox.Visible = false;

                table = new DataTable();
                table.Columns.AddRange(new DataColumn[] 
                { new DataColumn("Object", typeof(string)),
                    new DataColumn("Quantity", typeof(int)) });

                var objectArray = from o in DS.outpost._object
                                  select new { Name = o.obj_name, Id = o.obj_seq };

                DataRow row;
                foreach (var obj in objectArray)
                {
                    row = table.NewRow();
                    row[0] = string.Format("{0}({1})", obj.Name,obj.Id);
                    row[1] = (from a in DS.outpost.alert
                                      where a.dvrRow.obj_seq == obj.Id &&
                                      CheckedTimePickers(a)
                                      select a).Count();

                    table.Rows.Add(row);
                }
                columnArr = new string[] { "Object", "Quantity" };
                chartPanel.Controls.Clear();
                chartPanel.Controls.Add(GetChart(columnArr));
            }
        }

        bool CheckedTimePickers(OutpostDS.alertRow alertR)
        {
            if (allTimeCheckBox.Checked)
            {
                return true;
            }
            return alertR.alert_date >= dateTimePicker1.Value.Date && alertR.alert_date <= dateTimePicker2.Value.Date;

        }

        Chart GetChart(string[] arr)
        {
            WindowsCharting charting = new WindowsCharting();
            Chart chart = null;            
            chart = charting.GenerateChart(table, chartPanel.Width, chartPanel.Height, System.Drawing.KnownColor.ActiveCaption.ToString(), (string)chartTypeComboBox.SelectedItem, arr);
            chart.ChartAreas[0].Area3DStyle.Enable3D = enable3DcheckBox.Checked;
            return chart;
        }

        private void chartTypeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (allow)
            {
                chartPanel.Controls.Clear();
                chartPanel.Controls.Add(GetChart(columnArr));
            }
        }

        private void enable3DcheckBox_CheckedChanged(object sender, EventArgs e)
        {
            chartPanel.Controls.Clear();
            chartPanel.Controls.Add(GetChart(columnArr));
        }

        private void chartPanel_Resize(object sender, EventArgs e)
        {
            chartPanel.Controls.Clear();
            chartPanel.Controls.Add(GetChart(columnArr));
        }

        private void dateTimePicker_ValueChanged(object sender, EventArgs e)
        {
            regionsRadioButton_CheckedChanged(this, new EventArgs());
        }
    }
}
